# -*- coding:Utf-8 -*-
'''
Created on 24 nov. 2012

@author: fefoley
'''
import datetime, calendar

from timestrip import TimeStrip


class WorkDay(object):
    """
    une journée de travail avec une heure de début
    self.date : date using class datetime.date
    self.debut : time using class datetime.time
    self.hours : list of hours using class datetime.time
    self.mails : list of mails (class Mail) sent in the day
    self.sentmeetings : list of meetings sent in the day 
    self.meetings : list of meetings (class Meeting) of the day    
    self.dayoff : either None or strType ('CP', 'RTT', 'EXCEP', 'maladie')
    self.holiday : either None or name_of_holiday
    self.isWE : either None or self.SATURDAY or self.SUNDAY
    self.timestrips_on : working pieces of time
    self.timestrips_off : not working pieces of time
    """
    SATURDAY = calendar.SATURDAY
    SUNDAY = calendar.SUNDAY
    WEdico = {SATURDAY:'Samedi', SUNDAY:'Dimanche'}
    
    def __init__(self, date, tDebut=datetime.time(0, 0, 0)):
        self.date = date
        self.debut = tDebut
#        self.hours = [t for t in self.iterhours()]
        self.mails = []
        self.sentmeetings = []        
        self.meetings = []
        self.dayoff = None
        self.holiday = None
        if calendar.weekday(date.year, date.month, date.day) == calendar.SATURDAY:
            self.isWE = self.SATURDAY
        elif calendar.weekday(date.year, date.month, date.day) == calendar.SUNDAY:
            self.isWE = self.SUNDAY
        else : self.isWE = None
        self.timestrips_on = []
        self.timestrips_off = []
    
    def addmail(self, mail):
        """Add a mail to the mail list of the day"""
        self.mails.append(mail)
 
    def addsentmeeting(self,sentmeeting):
        """Add a meeting to the sentmeeting list of the day"""
        self.sentmeetings.append(sentmeeting)

    def addmeeting(self,meeting):
        """Add a meeting to the meeting list of the day"""
        self.meetings.append(meeting)
    
    def setdayoff(self, strtype):
        """set the day as off for latter use"""
        if self.dayoff:
            self.dayoff = self.dayoff + ' ' + strtype
        else :
            self.dayoff = strtype

    def setholiday(self, name=""):
        """set the day as holiday"""
        self.holiday = name
    


    def compute_timestrips(self):
        """Function to populate the timestrips_on and timestrip_off lists.  """
        for mail in self.mails:
            starttime = mail.getdatetime() - datetime.timedelta(minutes=mail.setup_minutes)
            endtime = mail.getdatetime() + datetime.timedelta(minutes=mail.setdown_minutes)
            kindof = TimeStrip.MAIL
            flag = mail.Subject
            evidence = mail
            label = ""
            adherence = mail.adh_minutes
            self.timestrips_on.append(TimeStrip(starttime, endtime, kindof, flag, evidence, label, adherence))
            
        for sentmeeting in self.sentmeetings:
            starttime = sentmeeting.getsentdatetime() - datetime.timedelta(minutes=sentmeeting.setup_minutes)     
            endtime = sentmeeting.getsentdatetime() + datetime.timedelta(minutes=sentmeeting.setdown_minutes)
            kindof = TimeStrip.MAIL
            flag = sentmeeting.Subject
            evidence = sentmeeting
            label = ""
            adherence = sentmeeting.adh_minutes
            self.timestrips_on.append(TimeStrip(starttime, endtime, kindof, flag, evidence, label, adherence))
        
        for meeting in self.meetings:
            starttime = meeting.Start
            endtime = meeting.End
            kindof = TimeStrip.MAIL
            flag = meeting.Subject
            evidence = meeting
            label = ""
            adherence = meeting.adh_minutes
            if meeting.BusyStatus == 'busy':
                self.timestrips_on.append(TimeStrip(starttime, endtime, kindof, flag, evidence, label, adherence))
            else:
                self.timestrips_off.append(TimeStrip(starttime, endtime, kindof, flag, evidence, label, adherence))
            
            
    def aggreg_timestrips(self):
        """function to aggregate the timestrips lists"""
        if not self.timestrips_on: return
        'sort the list by start time'
        self.timestrips_on.sort(key=lambda ts: str(ts.start))
        self.timestrips_on = TimeStrip.aggreg(self.timestrips_on)
 
        if not self.timestrips_off: return
        'sort the list by start time'
        self.timestrips_off.sort(key=lambda ts: str(ts.start))
        self.timestrips_off = TimeStrip.aggreg(self.timestrips_off)        
        ############################   TO COMPLETE ###############
    
            
    def __str__(self):
        strdayoff = ''
        if self.dayoff: strdayoff = " | dayoff: "+self.dayoff
        strholiday = ''
        if self.holiday: strholiday = " | holiday: "+self.holiday
        strWE = ''
        if self.isWE: strWE = " | WE: "+self.WEdico.get(self.isWE, '')  
        return "{} start at {}".format(self.date.strftime("%d/%m/%Y"), \
                                       self.debut.strftime("%H:%M")) \
                                       + strWE + strdayoff + strholiday       

    def __repr__(self):
        strdayoff = ''
        if self.dayoff: strdayoff = "-dayoff: "+self.dayoff    
        strholiday = ''
        if self.holiday: strholiday = "-holiday:"+self.holiday     
        strWE = ''
        if self.isWE: strWE = "-WE: "+self.WEdico.get(self.isWE, '')                         
        reprWD = "{}-{}".format(self.date.strftime("%d/%m/%Y"), \
                              self.debut.strftime("%H:%M")) \
                              + strWE + strdayoff + strholiday + '\n'
        for ts in self.timestrips_on:
            reprWD = reprWD + str(ts) + '\n'
        return reprWD 




####################################################################



if __name__ == "__main__":
    pass
    
        
        


         
    
